**free
// *  This is a simple example of a RESTful web service. It
// *  is called via the HTTP protocol, and accepts inputs from
// *  the URL that was used to call it.  For example:
// *
// *        http://example.com/cust/495
// *
// *  This means that you want to retrieve info about customer
// *  495. This program will then look up that customer info,
// *  and return the response in JSON format.
// *
// *    {
// *      "custno": 495,
// *      "name": "Acme Foods",
// *      "address": {
// *        "street": "123 MAIN STREET",
// *        "city": "BOCA RATON",
// *        "state": "FL"
// *        "postal": "12345-6789"
// *      }
// *    }
// *
// * To compile:
// *> CHGCURLIB SKWEBSRV
// *> CRTBNDRPG CUSTINFO2 SRCFILE(QRPGLESRC) DBGVIEW(*LIST)

ctl-opt OPTION(*SRCSTMT: *NODEBUGIO)
        DFTACTGRP(*NO) ACTGRP('RESTFUL');

dcl-f CUSTFILE disk keyed usage(*input);

dcl-pr getenv pointer extproc(*dclcase);
  var pointer value options(*string);
end-pr;

dcl-ds result qualified;
  custno packed(5: 0);
  name   varchar(30);
  dcl-ds address;
    street varchar(30);
    city   varchar(20);
    state  char(2);
    postal varchar(10);
  end-ds;
end-ds;

dcl-ds retError qualified;
  message varchar(200);
end-ds;

dcl-s pos  int(10);
dcl-s uri  varchar(5000);
dcl-s json varchar(5000);
dcl-c ID1  '/cust/';
dcl-c ID2  '/custinfo2/';

*inlr = *on;
uri = %str(getenv('REQUEST_URI'));

monitor;

  select;
  when %scan(ID1: uri) > 0;
    pos = %scan(ID1: uri) + %len(ID1);
  when %scan(ID2: uri) > 0;
    pos = %scan(ID2: uri) + %len(ID2);
  other;
    pos = 0;
  endsl;

  custno = %int(%subst(uri:pos));
on-error;
  retError.message = 'Invalid URI format.';
  data-gen retError %data(json)
                    %gen( 'YAJLDTAGEN'
                        : '{ +
                             "http status": 400, +
                             "write to stdout": true +
                           }');
  return;
endmon;

chain custno CUSTFILE;
if not %found;
  retError.message = 'Customer not found.';
  data-gen retError %data(json)
                    %gen( 'YAJLDTAGEN'
                        : '{ +
                             "http status": 404, +
                             "write to stdout": true +
                           }');
   return;
endif;

result.custno         = custno;
result.name           = name;
result.address.street = street;
result.address.city   = city;
result.address.state  = state;
result.address.postal = postal;

data-gen result %data(json)
                %gen( 'YAJLDTAGEN'
                    : '{ +
                         "http status": 200, +
                         "write to stdout": true +
                       }');

*inlr = *on; 